package cn.zjh.user.service.impl;

import cn.zjh.base.constants.BaseConstants;
import cn.zjh.base.exception.BusinessException;
import cn.zjh.user.service.service.impl.BaseServiceImpl;
import cn.zjh.base.utils.MD5Utils;
import cn.zjh.base.utils.StrUtils;
import cn.zjh.base.vo.RegisterVo;
import cn.zjh.user.domain.Logininfo;
import cn.zjh.user.domain.User;
import cn.zjh.user.mapper.LogininfoMapper;
import cn.zjh.user.mapper.UserMapper;
import cn.zjh.user.service.IUserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zjh
 * @since 2022-07-06
 */
@Service
public class UserServiceImpl extends BaseServiceImpl<User> implements IUserService {
    
    
    @Autowired
    private UserMapper userMapper;
    
    @Autowired
    private RedisTemplate redisTemplate;


    @Autowired
    private LogininfoMapper logininfoMapper;

    /**
    * @Title: phoneRegister
    * @Description: 手机注册
    * @Author: Mr.Zhang
    * @Version: 1.0
    * @Date:  2022/7/7 0:25
    * @Parameters:
    * @Return
    */
    @Override
    public void phoneRegister(RegisterVo vo) {
        String imageCodeKey = vo.getImageCodeKey();
        String confirPassword = vo.getConfirPassword();
        String phone = vo.getPhone();
        String password = vo.getPassword();
        String smsCodeValue = vo.getSmsCodeValue();
        String imageCodeValue = vo.getImageCodeValue();
        //参数校验
        if (StringUtils.isEmpty(phone)
                ||StringUtils.isEmpty(password)
                ||StringUtils.isEmpty(smsCodeValue)
                ||StringUtils.isEmpty(imageCodeKey)
                ||StringUtils.isEmpty(imageCodeValue)
                ||StringUtils.isEmpty(confirPassword)){
            throw new BusinessException("参数不能为空，请仔细填写！");
        }

        //密码是否一致
        if (!vo.getPassword().equals(vo.getConfirPassword())){
            throw new BusinessException("两次密码不一致，请重新填写！");
        }

        //检验验证码是否过期
        //拼接redisKey
        String smsCodeKey = BaseConstants.VerifyCodeConstant.BUSINESS_REGISTER_PREFIX+vo.getPhone();
        Object smsCodeValueTmp = redisTemplate.opsForValue().get(smsCodeKey);
        if (smsCodeValueTmp == null){
            throw new BusinessException("验证码已过期，请重新输入验证码！");
        }

        //判断验证码是否正确
        String[] splitSmsCode = smsCodeValueTmp.toString().split(":");
        String smsCode = splitSmsCode[1];
        if(!smsCode.equalsIgnoreCase(vo.getSmsCodeValue())){
            throw new BusinessException("验证码不正确，请重新获取并输入！");
        }

        //保存到user&&logininfo
        //将register中的数据保存到user中
        User user = registerVo2User(vo);
        //再将user中的数据保存到logininfo中
        Logininfo logininfo = user2Logininfo(user);

        logininfoMapper.save(logininfo);//返回主键id

        //设置user中的logininfo_id字段
        user.setLogininfoId(logininfo.getId());
        userMapper.save(user);
    }


    /**
     * 封装User，将register中的值设置到user中
     * @param vo
     * @return
     */
    public User registerVo2User(RegisterVo vo){
        User user = new User();
        //手机号
        user.setPhone(vo.getPhone());
        //用户名初始化，默认为手机
        user.setUsername(vo.getPhone());
        //密码加严加密
        String salt = StrUtils.getComplexRandomString(32);
        String Md5Password = MD5Utils.encrypByMd5(vo.getPassword() + salt);
        //密码
        user.setPassword(Md5Password);
        //盐值
        user.setSalt(salt);
        return user;
    }


    /**
     * 封装logininfo，将user中的值设置到logininfo中
     * @param user
     * @return
     */
    public Logininfo user2Logininfo(User user){
        Logininfo logininfo = new Logininfo();
        BeanUtils.copyProperties(user,logininfo);
        //设置注册类型为用户
        logininfo.setType(1);
        return logininfo;
    }



}
